//
//  AppDelegate.m
//  HZHome
//
//  Created by SunShine on 13-9-19.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "AppDelegate.h"

#import "LandingViewController.h"

#import "IconPagesViewController.h"
#import "LoginViewController.h"
#import "WebViewController.h"
#import "TravelRouteViewController.h"
#import "SearchViewController.h"

#import "PersonalCenterViewController.h"
#import "SubscribeViewController.h"
#import "MoreViewController.h"

@implementation AppDelegate


- (void)interIndexViewController
{
    NSMutableArray *mutableArray = [[NSMutableArray alloc] init];

    LandingViewController *viewController1 = [[LandingViewController alloc] init];
    SubscribeViewController *viewController2 = [[SubscribeViewController alloc] init];

    SearchViewController *viewController3 = [[SearchViewController alloc]initWithNibName:@"SearchViewController" bundle:nil];
    
    LoginViewController *viewController4 = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
    MoreViewController *viewController5 = [[MoreViewController alloc]initWithNibName:@"MoreViewController" bundle:nil];

    
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
    UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
    UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:viewController4];
    UINavigationController *nav5 = [[UINavigationController alloc] initWithRootViewController:viewController5];

    [mutableArray addObject:nav1];
    [mutableArray addObject:nav2];
    [mutableArray addObject:nav3];
    [mutableArray addObject:nav4];
    [mutableArray addObject:nav5];

    
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = mutableArray;
    self.tabBarController.delegate = self;
    self.tabBarController.selectedIndex = 0;
    
    UITabBar *tabBar = self.tabBarController.tabBar;
    UITabBarItem *aTabBarItem = [tabBar.items objectAtIndex:0];
    UITabBarItem *bTabBarItem = [tabBar.items objectAtIndex:1];
    UITabBarItem *cTabBarItem = [tabBar.items objectAtIndex:2];
    UITabBarItem *dTabBarItem = [tabBar.items objectAtIndex:3];
    UITabBarItem *eTabBarItem = [tabBar.items objectAtIndex:4];

    
    aTabBarItem.title = @"海珠主页";
    bTabBarItem.title = @"海珠预约";
    cTabBarItem.title = @"查找";
    dTabBarItem.title = @"登录";
    eTabBarItem.title = @"更多";

    
    [aTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"tab_home_paress.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_home_unparess.png"]];
    [bTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"tab_appointment_paress.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_appointment_unparess.png"]];
    [cTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"海珠家园二期【首页1、2】_07.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"海珠家园二期【首页1、2】_07 (2).png"]];
    [dTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"tab_login_paress.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_login_unparess.png"]];
    [eTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"海珠家园二期【首页】20140516_13.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"海珠家园二期【首页】201405116_11_11.png"]];

    
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor, nil] forState:UIControlStateSelected];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
    
    UIImage* tabBarBackground = [UIImage imageNamed:@"tabarBack.png"];
    [[UITabBar appearance] setBackgroundImage:[tabBarBackground resizableImageWithCapInsets:UIEdgeInsetsZero]];
//    [[UITabBar appearance] setBackgroundColor:[UIColor blackColor]];

//    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"navbar_button_bg_pressed.9.png"]];


    self.window.rootViewController = self.tabBarController;
}


- (void)successLoginWithIndexViewController
{
    NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
    
    LandingViewController *viewController1 = [[LandingViewController alloc] init];
    SubscribeViewController *viewController2 = [[SubscribeViewController alloc] init];
    
    SearchViewController *viewController3 = [[SearchViewController alloc]initWithNibName:@"SearchViewController" bundle:nil];
    
    PersonalCenterViewController *viewController4 = [[PersonalCenterViewController alloc] init];
    MoreViewController *viewController5 = [[MoreViewController alloc]initWithNibName:@"MoreViewController" bundle:nil];

    
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
    UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
    UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:viewController4];
    UINavigationController *nav5 = [[UINavigationController alloc] initWithRootViewController:viewController5];
    
    [mutableArray addObject:nav1];
    [mutableArray addObject:nav2];
    [mutableArray addObject:nav3];
    [mutableArray addObject:nav4];
    [mutableArray addObject:nav5];
    
    
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = mutableArray;
    self.tabBarController.delegate = self;
    self.tabBarController.selectedIndex = 0;
    
    UITabBar *tabBar = self.tabBarController.tabBar;
    UITabBarItem *aTabBarItem = [tabBar.items objectAtIndex:0];
    UITabBarItem *bTabBarItem = [tabBar.items objectAtIndex:1];
    UITabBarItem *cTabBarItem = [tabBar.items objectAtIndex:2];
    UITabBarItem *dTabBarItem = [tabBar.items objectAtIndex:3];
    UITabBarItem *eTabBarItem = [tabBar.items objectAtIndex:4];
    
    
    aTabBarItem.title = @"海珠主页";
    bTabBarItem.title = @"海珠预约";
    cTabBarItem.title = @"查找";
    dTabBarItem.title = @"我的海珠";
    eTabBarItem.title = @"更多";
    
    
    [aTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"tab_home_paress.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_home_unparess.png"]];
    [bTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"tab_appointment_paress.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_appointment_unparess.png"]];
    [cTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"海珠家园二期【首页1、2】_07.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"海珠家园二期【首页1、2】_07 (2).png"]];
    [dTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"tab_login_paress.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab_login_unparess.png"]];
    [eTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"海珠家园二期【首页】20140516_13.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"海珠家园二期【首页】201405116_11_11.png"]];
    
    
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor, nil] forState:UIControlStateSelected];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
    
    UIImage* tabBarBackground = [UIImage imageNamed:@"tabarBack.png"];
    [[UITabBar appearance] setBackgroundImage:[tabBarBackground resizableImageWithCapInsets:UIEdgeInsetsZero]];
    //    [[UITabBar appearance] setBackgroundColor:[UIColor blackColor]];
    
    //    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"navbar_button_bg_pressed.9.png"]];
    
    
    self.window.rootViewController = self.tabBarController;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
{
    if (tabBarController.selectedIndex == 2) {
        UITabBar *tabBar = tabBarController.tabBar;
        UITabBarItem *tabBarItem = [tabBar.items objectAtIndex:2];
        tabBarItem.title = @"查找";
        viewController.title = @"查找";
    }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(successLoginWithIndexViewController) name:@"successLoginWithIndexViewController" object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(interIndexViewController) name:@"interIndexViewController" object:nil];

    [self interIndexViewController];
    //定义全局外观
    [[UISearchBar appearance] setBackgroundColor:[UIColor clearColor]];
    [[UISearchBar appearance] setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], UITextAttributeTextColor, nil]];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
    if (RunTimeOSVersion >= 7.0) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        [[UITableView appearance] setSeparatorInset:UIEdgeInsetsZero];
    }
#endif
    
    //设备唯一标识
    NSString *Id = getDeviceIdentifier();
    NSLog(@"当前设备唯一标识:%@", Id);
    
    //新版本执行通知
    [self showNewVertionNotice];
    
    [self.window makeKeyAndVisible];
    
    //异常捕捉
    [self installUncaughtExceptionHandler];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma mark - Init Method
-(void)showNewVertionNotice
{
    NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSLog(@"当前应用版本号:%@", currentVersion);
    NSString *storedVersion = [DataStoreService loadFromDefaults:LAST_APP_VERSION_KEY];
    if (storedVersion && [storedVersion isEqualToString:currentVersion]) {
        NSLog(@"当前版本非第一次运行，跳过提示");
    } else {
        NSLog(@"当前版本第一次运行，显示提示");
        //执行提示
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"注册免流量" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [alert show];
        //保存当前版本号
        [DataStoreService saveToDefaults:LAST_APP_VERSION_KEY data:currentVersion];
    }
}

//捕捉异常
- (void)installUncaughtExceptionHandler{
    [NdUncaughtExceptionHandler setDefaultHandler];
	InstallUncaughtExceptionHandler();
}

@end
